package com.wty.shop.util;

import com.wty.shop.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

/**
 * @Author 34355
 * @Create 2018-01-29-19:47
 */
@Slf4j
public class RedisShardedPoolUtil {
    /**
     *
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key,int exTime){
        ShardedJedis shardedJedis=null;
        Long result=null;
        try {
            shardedJedis= RedisShardedPool.getJedis();
            result=shardedJedis.expire(key,exTime);
        } catch (Exception e) {
            log.error("expire key{},error",key,e);
            RedisShardedPool.returnBorkenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }


    /**
     * 设置key value
     * @param key
     * @param value
     * @return
     */
    public static String set(String key,String value){
        ShardedJedis shardedJedis=null;
        String result=null;

        try {
            shardedJedis= RedisShardedPool.getJedis();
            result=shardedJedis.set(key,value);
        } catch (Exception e) {
            log.error("set key{},value{},error",key,value,e);
            RedisShardedPool.returnBorkenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }


    /**
     *
     * @param key
     * @param value
     * @return
     */

    public static Long setnx(String key,String value){
        ShardedJedis shardedJedis=null;
        Long result=null;

        try {
            shardedJedis= RedisShardedPool.getJedis();
            result=shardedJedis.setnx(key,value);
        } catch (Exception e) {
            log.error("setnx key{},value{},error",key,value,e);
            RedisShardedPool.returnBorkenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    /**
     * 设置key/value 并带有存活时间（秒）
     * @param key
     * @param exTime
     * @param value
     * @return
     */
    public static String setEx(String key,int exTime,String value){
        ShardedJedis shardedJedis=null;
        String result=null;

        try {
            shardedJedis=RedisShardedPool.getJedis();
            result=shardedJedis.setex(key,exTime,value);
        } catch (Exception e) {
            log.error("setEx key{},value{} error",key,value,e);
            RedisShardedPool.returnBorkenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;

    }
    /**
     * 根据key获得value
     * @param key
     * @return
     */
    public static String get(String key){
        ShardedJedis jedis=null;
        String result=null;

        try {
            jedis= RedisShardedPool.getJedis();
            result=jedis.get(key);
        } catch (Exception e) {
            log.error("get key{},error",key,e);
            RedisShardedPool.returnBorkenResource(jedis);
            return result;
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    /**
     * 设置新的值，返回老的值
     * @param key
     * @return
     */
    public static String getSet(String key,String value){
        ShardedJedis jedis=null;
        String result=null;

        try {
            jedis= RedisShardedPool.getJedis();
            result=jedis.getSet(key,value);
        } catch (Exception e) {
            log.error("getSet key{},value{},error",key,value,e);
            RedisShardedPool.returnBorkenResource(jedis);
            return result;
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    /**
     * 删除
     * @param key
     * @return
     */
    public static Long del(String key){
        ShardedJedis shardedJedis=null;
        Long result=null;
        try {
            shardedJedis= RedisShardedPool.getJedis();
            result=shardedJedis.del(key);
        } catch (Exception e) {
            log.error("del key{},error",key,e);
            RedisShardedPool.returnBorkenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }
}
